package fourFragment.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.xuegengwang.xuegengwang.R;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import common.BaseFragment;
import common.Constant;
import fourFragment.bean.MyFocusCollegeBean;
import https.InterNetController;
import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;
import mainFragment.activity.CollegeIndex;
import mainFragment.adapter.RMGXLvAdapter;
import mainFragment.bean.HotCollegeBean;
import view.MyPtrRefresh_LoadView;

/**
 * Created by Administrator on 2017/4/7.
 * 关注的高校
 */
@SuppressLint("ValidFragment")
public class MyFocusCollege extends BaseFragment {
    private View view;
    private MyPtrRefresh_LoadView my_focus_college_refreshLayout;
    private ListView my_focus_college_lv;
    private int page;
    private final int REFRESH = 0;
    private final int ONLOAD = 1;

    private ArrayList<HotCollegeBean.CollegeListBean> arryList;
    private RMGXLvAdapter rmgxLvAdapter;

    private static final String TAG = "MyFocusCollege";

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.ERROR_CODE_SUCCESS://成功返回
                    Gson gson = new Gson();
                    MyFocusCollegeBean myFocusCollegeBean = gson.fromJson((String) msg.obj, MyFocusCollegeBean.class);
                    if (myFocusCollegeBean != null) {
                        List<MyFocusCollegeBean.CollegeListBean> collegeList = myFocusCollegeBean.getCollegeList();
                        if (collegeList != null) {
                            if(msg.arg1 == REFRESH){
                                arryList.clear();
                            }
                            if (collegeList.size() > 0) {
                                for (int i = 0; i < collegeList.size(); i++) {
                                    HotCollegeBean.CollegeListBean collegeListBean = new HotCollegeBean.CollegeListBean();
                                    collegeListBean.setIcon(collegeList.get(i).getIcon());
                                    collegeListBean.setId(collegeList.get(i).getId());
                                    collegeListBean.setLowest_art(collegeList.get(i).getLowest_art());
                                    collegeListBean.setLowest_science(collegeList.get(i).getLowest_science());
                                    collegeListBean.setSchool_name(collegeList.get(i).getSchool_name());
                                    collegeListBean.setTag(collegeList.get(i).getTag());
                                    arryList.add(collegeListBean);
                                }
                            }
                            else{
                                if(msg.arg1 == ONLOAD){
                                    page--;
                                }
                            }
                            rmgxLvAdapter.notifyDataSetChanged();
                        }
                        else{
                            if(msg.arg1 == ONLOAD){
                                page--;
                            }
                        }
                    }
                    break;
                case Constant.ERROR_CODE_FINISH:
                    my_focus_college_refreshLayout.OnRefreshComplete();
                    break;
                default:
                    break;
            }
        }
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.my_focus_college, null);
        initView();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        getDataFromInterNet(page = 1, REFRESH);
    }

    private void initView() {
        my_focus_college_refreshLayout = (MyPtrRefresh_LoadView) view.findViewById(R.id.my_focus_college_refreshLayout);
        my_focus_college_lv = (ListView) view.findViewById(R.id.my_focus_college_lv);

        my_focus_college_refreshLayout.setPtrHandler(new PtrDefaultHandler2() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                getDataFromInterNet(++page, ONLOAD);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                getDataFromInterNet(page = 1, REFRESH);
            }
        });
        arryList = new ArrayList<>();
        rmgxLvAdapter = new RMGXLvAdapter(getMyActivity(),arryList);
        my_focus_college_lv.setAdapter(rmgxLvAdapter);
        my_focus_college_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent i = new Intent();
                i.putExtra("id", arryList.get(position).getId());
                i.putExtra("name", arryList.get(position).getSchool_name());
                i.setClass(getMyActivity(), CollegeIndex.class);
                startActivityForResult(i,1);
            }
        });

    }

    private void getDataFromInterNet(int page, int flag) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("type", "college");
        hashMap.put("page", String.valueOf(page));
        new InterNetController(getMyActivity(), Constant.ATTENTIONINFO, handler, hashMap, flag,false);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == 1){
            boolean changeFocus = data.getBooleanExtra("changeFocus", false);
            if(changeFocus){
                getDataFromInterNet(page = 1, REFRESH);
            }
        }
    }
}
